package one.stand.service.impl;

import com.jmp.autocofiguration.util.OssUtil2;
import one.stand.mapper.WebsiteMapperExt;
import one.stand.model.Website;
import one.stand.model.WebsiteModel;
import one.stand.service.WebsiteService;
import one.stand.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class WebsiteServiceImpl implements WebsiteService {
    @Autowired
    private WebsiteMapperExt websiteMapperExt;

    @Override
    @Transactional
    public Integer saveEmpty(Integer companyId) {
        Website model = new Website();
        model.setCompanyId(companyId);
        model.setCreateTime(DateUtil.currentDate());
        model.setIsDel(0);
        websiteMapperExt.insertSelective(model);
        return model.getWebsiteId();
    }

    @Override
    @Transactional
    public void update(Website model) {

        model.setPhotos(OssUtil2.getWebsiteUrlReplace(model.getPhotos()));
        model.setCompanyIntroduce(OssUtil2.getWebsiteUrlReplace(model.getCompanyIntroduce()));
        model.setGoodsIntroduce(OssUtil2.getWebsiteUrlReplace(model.getGoodsIntroduce()));

        websiteMapperExt.updateByPrimaryKeySelective(model);
    }

    @Override
    public Website getByCompanyId(Integer companyId) {
        return websiteMapperExt.selectByCompanyId(companyId);
    }

    @Override
    public WebsiteModel getModelByCompanyId(Integer companyId) {
        return websiteMapperExt.select(new WebsiteModel() {{
            setCompanyId(companyId);
        }});
    }
}
